<template>
  <div class="user">
    <page-search
      :searchFormConfig="searchFormConfig"
      @resetBtnClick="handleResetClick"
      @searchBtnClick="handleSearchClick"
    />
    <page-content
      ref="pageContentRef"
      :contentTableConfig="contentTableConfig"
      pageName="users"
      @newBtnClick="handleNewData"
      @editBtnClick="handleEditData"
    >
      <template #status="scope">
        <el-button
          @click="handleUpdataStatus(scope.row)"
          size="mini"
          :type="scope.row.enable ? 'success' : 'danger'"
        >
          {{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
    </page-content>
    <page-modal
      ref="pageModalRef"
      :modalConfig="modalConfigRef"
      :defaultInfo="defaultInfo"
      pageName="users"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import PageSearch from '@/components/page-search'
import PageContent from '@/components/page-content'
import PageModal from '@/components/page-modal'
import { searchFormConfig } from './config/search.config'
import { contentTableConfig } from './config/content.config'
import { usePageSearch } from '@/hooks/use-page-search'
import { modalConfig } from './config/modal.config'
import { usePageModal } from '@/hooks/use-page-modal'
import { useStore } from '@/store'
import { editPageData } from '@/service/main/system/system'
import { ElMessage } from 'element-plus'

export default defineComponent({
  name: 'user',
  components: {
    PageSearch,
    PageContent,
    PageModal
  },
  setup() {
    const [pageContentRef, handleResetClick, handleSearchClick] = usePageSearch()
    // pageModal相关的hooks
    const newCallback = () => {
      modalConfig.formItems.find((item) => item.field === 'password')!.isHidden = false
    }
    const editCallback = () => {
      modalConfig.formItems.find((item) => item.field === 'password')!.isHidden = true
    }
    const [handleNewData, handleEditData, pageModalRef, defaultInfo] = usePageModal(
      newCallback,
      editCallback
    )

    // 动态添加部门和角色列表
    const store = useStore()
    const modalConfigRef = computed(() => {
      const departmentItem = modalConfig.formItems.find((item) => item.field === 'departmentId')
      departmentItem!.options = store.state.entireDepartment.map((item) => ({
        title: item.name,
        value: item.id
      }))
      const roleItem = modalConfig.formItems.find((item) => item.field === 'roleId')
      roleItem!.options = store.state.entireRole.map((item) => ({
        title: item.name,
        value: item.id
      }))
      return modalConfig
    })

    // 修改状态
    const handleUpdataStatus = async (item: any) => {
      const result = await editPageData(`/users/${item.id}`, {
        enable: item.enable === 0 ? 1 : 0
      })
      if (result.code === 0) {
        item.enable = item.enable === 0 ? 1 : 0
      } else {
        ElMessage.error('修改状态错误！')
      }
    }

    return {
      searchFormConfig,
      contentTableConfig,
      pageContentRef,
      handleResetClick,
      handleSearchClick,
      modalConfigRef,
      handleNewData,
      handleEditData,
      pageModalRef,
      defaultInfo,
      handleUpdataStatus
    }
  }
})
</script>

<style lang="less" scoped></style>
